package com.zh.note.huawei.onehundred.AI处理器组合;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 题目：
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String inputString = sc.nextLine();
        // int target = sc.nextInt();
        String inputString = "0 1 4 5 6 7";
        int target = 1;
        String[] strs = inputString.split(" ");
        List<Integer> lists = Arrays.asList(strs).stream().filter(str -> !str.isEmpty())
                .map(s -> Integer.parseInt(s))
                .collect(Collectors.toList());
        String res = findSubsets(lists, target);
        System.out.println(res);

    }

    private static String findSubsets(List<Integer> lists, int target) {
        // 存储小于4的元素
        List<Integer> smallNums = new ArrayList<>();
        // 存储大于4的元素
        List<Integer> largeNums = new ArrayList<>();
        for (Integer num : lists) {
            if (num < 4) {
                smallNums.add(num);

            } else {
                largeNums.add(num);
            }
        }
        List<List<Integer>> subsets = new ArrayList<>();
        int smallSize = smallNums.size();
        int largeSize = largeNums.size();
        switch (target) {
            case 1:
                handleTarget1(smallNums, largeNums, smallSize, largeSize, subsets);
                break;
            case 2:
                handleTarget2(smallNums, largeNums, smallSize, largeSize, subsets);
                break;
            case 4:
                handleTarget4(smallNums, largeNums, smallSize, largeSize, subsets);
                break;
            case 8:
                handleTarget8(smallNums, largeNums, smallSize, largeSize, subsets);
                break;
        }
        return subsets.toString();

    }


    private static void handleTarget1(List<Integer> smallNums, List<Integer> largerNums, int smallSize, int largerSize, List<List<Integer>> subsets) {
        if (smallSize == 1 || largerSize == 1) {
            if (smallSize == 1) {
                dfs(smallNums, 0, 1, new ArrayList<>(), subsets);
            }
            if (largerSize == 1) {
                dfs(largerNums, 0, 1, new ArrayList<>(), subsets);
            }
        } else if (smallSize == 3 || largerSize == 3) {
            if (smallSize == 3) {
                dfs(smallNums, 0, 1, new ArrayList<>(), subsets);
            }
            if (largerSize == 3) {
                dfs(largerNums, 0, 1, new ArrayList<>(), subsets);
            }

        } else if (smallSize == 2 || largerSize == 2) {
            if (smallSize == 2) {
                dfs(smallNums, 0, 1, new ArrayList<>(), subsets);
            }
            if (largerSize == 2) {
                dfs(largerNums, 0, 1, new ArrayList<>(), subsets);
            }

        }
    }

    private static void handleTarget2(List<Integer> smallNums, List<Integer> largeNums, int smallSize, int largeSize, List<List<Integer>> subsets) {

        if (smallSize == 2 || largeSize == 2) {
            if (smallSize == 2) dfs(smallNums, 0, 2, new ArrayList<>(), subsets);
            if (largeSize == 2) dfs(largeNums, 0, 2, new ArrayList<>(), subsets);
        } else if (smallSize == 4 || largeSize == 4) {
            if (smallSize == 4) dfs(smallNums, 0, 2, new ArrayList<>(), subsets);
            if (largeSize == 4) dfs(largeNums, 0, 2, new ArrayList<>(), subsets);
        } else if (smallSize == 3 || largeSize == 3) {
            if (smallSize == 3) dfs(smallNums, 0, 2, new ArrayList<>(), subsets);
            if (largeSize == 3) dfs(largeNums, 0, 2, new ArrayList<>(), subsets);
        }
    }

    public static void handleTarget4(List<Integer> smallNums,
                                     List<Integer> largeNums,
                                     int smallSize, int largeSize, List<List<Integer>> subsets) {
        if (smallSize == 4 || largeSize == 4) {
            if (smallSize == 4) subsets.add(smallNums);
            if (largeSize == 4) subsets.add(largeNums);
        }
    }

    public static void handleTarget8(List<Integer> smallNums,
                                     List<Integer> largeNums,
                                     int smallSize, int largeSize, List<List<Integer>> subsets) {
        if (smallSize == 4 && largeSize == 4) {
            subsets.add(Stream.concat(smallNums.stream(), largeNums.stream())
                    .collect(Collectors.toList()));
        }
    }

    /**
     * 取k个数的组合问题
     *
     * @param nums
     * @param startIndex
     * @param k
     * @param path
     * @param subsets
     */
    private static void dfs(List<Integer> nums, int startIndex, int k, List<Integer> path, List<List<Integer>> subsets) {
        if (path.size() == k) {
            subsets.add(new ArrayList<>(path));
        }
        for (int i = startIndex; i < nums.size(); i++) {
            path.add(nums.get(i));
            dfs(nums, i + 1, k, path, subsets);
            path.remove(path.size() - 1);
        }

    }

}
